﻿using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Model.Entities;
using ActivitiesControl.Domain.Model.VOs;

namespace ActivitiesControl.Domain.Services.Impls
{
	[ExceptionMessageHandler]
	internal class UserService
		: BaseService, IUserService
	{
		private UserHandler handler;
		
		public override event MessageHandler MessageRaised = delegate(object sender, MessageEventArgs e) { };

		public UserService()
		{
			handler = new UserHandler(UserRepository);
		}

		internal override void RaiseMessage(object sender, MessageEventArgs e)
		{
			MessageRaised(sender, e);
		}

		public void UpdateUserPassword(string login, string password, string newPassword, string newPasswordConfirmation)
		{
			handler.ChangePassword(login, password, newPassword, newPasswordConfirmation);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Alteração de senha", "Senha alterada com sucesso.", MessageType.Ok) });
		}

		public bool ValidateUser(string login, string password, out User loggedUser)
		{
			bool validated = handler.Validate(login, password, out loggedUser);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Login", "Usuário logado.", MessageType.Ok) });
			return validated;
		}

		public User GetUserByLogin(string login)
		{
			return UserRepository.GetUserByLogin(login);
		}
	}
}